// Copyright (c) 2012, yuno. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
package sugarmintcandy.widgetwindow;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class CloseButtonDrawable extends Drawable {
    private Paint mPaint;
    private Path mPath;

    @Override
    public void draw(Canvas canvas) {
	if (mPaint == null) {
	    Rect bounds = getBounds();
	    mPaint = new Paint();
	    mPaint.setAntiAlias(true);
	    mPaint.setColor(-1);
	    mPaint.setStyle(Paint.Style.STROKE);
	    mPaint.setStrokeCap(Paint.Cap.BUTT);
	    mPaint.setStrokeWidth((bounds.width() + bounds.height()) * 0.07f);
	    mPaint.setShadowLayer(1, 0, 0, 0xff000000);
	    for (int state : getState()) {
		if (state == android.R.attr.state_pressed) {
		    mPaint.setColor(0xff808080);
		}
	    }
	}
	if (mPath == null) {
	    Rect bounds = getBounds();
	    float left = bounds.left + bounds.width() * 0.18f;
	    float top = bounds.top + bounds.height() * 0.18f;
	    float right = bounds.right - bounds.width() * 0.18f;
	    float bottom = bounds.bottom - bounds.height() * 0.18f;
	    mPath = new Path();
	    mPath.moveTo(left, top);
	    mPath.lineTo(right, bottom);
	    mPath.moveTo(left, bottom);
	    mPath.lineTo(right, top);
	    mPaint.setStrokeWidth((bounds.width() + bounds.height()) * 0.07f);
	}
	canvas.drawPath(mPath, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
	super.onBoundsChange(bounds);
	mPath = null;
    }

    @Override
    protected boolean onStateChange(int[] state) {
	super.onStateChange(state);
	mPaint = null;
	invalidateSelf();
	return true;
    }

    @Override
    public int getOpacity() {
	return 255;
    }

    @Override
    public boolean isStateful() {
	return true;
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public ConstantState getConstantState() {
	return new MyConstantState();
    }

    private static class MyConstantState extends ConstantState {
	@Override
	public int getChangingConfigurations() {
	    return 0;
	}

	@Override
	public Drawable newDrawable() {
	    return new CloseButtonDrawable();
	}
    }
}
